package com.zhuxiang.test1;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class StudentManager {

    private Scanner sc = new Scanner(System.in);
    Student stu = new Student();
    List<Student> list = new ArrayList<>();
    String path = "student.txt";
    public static void main(String[] args) {
        StudentManager sm = new StudentManager();
        sm.menu();
    }

    /**
     * 菜单方法，根据用户输入的不同，调用不同的方法
     */
    public void menu(){
        init();
        do {
            System.out.println("=======学生管理系统=========");
            System.out.println("1.增加学生信息");
            System.out.println("2.删除学生信息");
            System.out.println("3.修改学生信息");
            System.out.println("4.根据学号查找");
            System.out.println("5.查询所有信息");
            System.out.println("0.退出系统");
            System.out.println("==========================");
            int a = sc.nextInt();
            switch (a){
                case 1:	add();break;
                case 2:	delete();break;
                case 3: update();break;
                case 4: findById();break;
                case 5: findAll();break;
                default:
                    System.out.println("退出系统");
                    System.exit(0);
            }
        }while (true);
    }

    public void init(){
        File file = new File(path);
        if (!file.exists()){
            writeData();
        }
    }

    /**
     * 将list中的内容存到文件中
     */
    public void writeData(){
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream(path));
            oos.writeObject(list);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != oos){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 从文件中读取内容存到list中
     */
    public void readData() {
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream(path));
            list = (List<Student>) ois.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (null != ois) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    /**
     * 查询所有信息
     */
    private void findAll(){
        readData();
        if (0 == list.size()){
            System.out.println("尚未录入任何信息");
        }else {
            System.out.println("查询到的信息如下：");
            for (int i = 0; i < list.size(); i++) {
                stu = list.get(i);
                System.out.println(stu.toString());
            }
        }
    }

    /**
     * 根据id查询学生信息
     */
    private void findById() {
        readData();
        System.out.println("请输入学生id");
        int id = sc.nextInt();
        int index= isTrue(id);
        if (-1 != index){
            stu = list.get(index);
            System.out.println("查询到的学生信息为：");
            System.out.println(stu.toString());
        }else {
            System.out.println("输入的id有误");
        }
    }

    /**
     * 公有方法 根据id判断是否信息已存在
     * @param id
     * @return
     */
    private int isTrue(int id){
        readData();
        for (int i = 0; i < list.size(); i++) {
            stu = list.get(i);
            if (id == stu.getId()){
                return i;
            }
        }
        return -1;
    }

    /**
     * 更新学生信息方法
     */
    private void update() {
        System.out.println("请输入学生id");
        int id = sc.nextInt();
        int index = isTrue(id);
        if (-1 != index){
            System.out.println("请输入学生姓名");
            String name = sc.next();
            System.out.println("请输入学生年纪");
            int age = sc.nextInt();
            stu = new Student(id,name,age);
            list.set(index,stu);
            writeData();
            System.out.println("信息修改成功");
        }else {
            System.out.println("输入的id可能有误");
        }
    }

    /**
     * 新增学生信息方法
     */
    private void add(){
        int id = 0;
        while (true){
            System.out.println("请输入学生id");
            id = sc.nextInt();
            //先判断该id是否已存在
           int index = isTrue(id);
           if (-1 != index){
               System.out.println("该id已存在");
           }else {
               break;
           }
        }
        System.out.println("请输入学生姓名");
        String name = sc.next();
        System.out.println("请输入学生年纪");
        int age = sc.nextInt();
        stu = new Student(id,name,age);
        if (id <= 0 || age <= 0 || age >=100){
            System.out.println("信息输入有误，添加失败！");
        }else {
            list.add(stu);
            writeData();
            System.out.println("信息添加成功");
        }
    }

    /**
     * 删除学生信息方法
     */
    private void delete() {
        System.out.println("请输入删除学生的id");
        int id = sc.nextInt();
        int index = isTrue(id);
        if (-1 != index){
            list.remove(index);
           writeData();
            System.out.println("删除成功");
        }else {
            System.out.println("输入学号可能有误");
        }
    }
}
